// MooTools: the javascript framework.
// Load this file's selection again by visiting: http://mootools.net/more/417ab68a672f38ea1c04efab47af2ff7
// Or build this file again with packager using: packager build More/Assets
/*
---

script: More.js

name: More

description: MooTools More

license: MIT-style license

authors:
  - Guillermo Rauch
  - Thomas Aylott
  - Scott Kyle
  - Arian Stolwijk
  - Tim Wienk
  - Christoph Pojer
  - Aaron Newton
  - Jacob Thornton

requires:
  - Core/MooTools

provides: [MooTools.More]

...
*/

MooTools.More = {
  'version': '1.3.2.1',
  'build': 'e586bcd2496e9b22acfde32e12f84d49ce09e59d'
};


/*
---

script: Assets.js

name: Assets

description: Provides methods to dynamically load JavaScript, CSS, and Image files into the document.

license: MIT-style license

authors:
  - Valerio Proietti

requires:
  - Core/Element.Event
  - /MooTools.More

provides: [Assets]

...
*/

var Asset = {

  javascript: function(source, properties){
    if (!properties) properties = {};

    var script = new Element('script', {src: source, type: 'text/javascript'}),
      doc = properties.document || document,
      loaded = 0,
      loadEvent = properties.onload || properties.onLoad;

    var load = loadEvent ? function(){ // make sure we only call the event once
      if (++loaded == 1) loadEvent.call(this);
    } : function(){};

    delete properties.onload;
    delete properties.onLoad;
    delete properties.document;

    return script.addEvents({
      load: load,
      readystatechange: function(){
        if (['loaded', 'complete'].contains(this.readyState)) load.call(this);
      }
    }).set(properties).inject(doc.head);
  },

  css: function(source, properties){
    if (!properties) properties = {};

    var link = new Element('link', {
      rel: 'stylesheet',
      media: 'screen',
      type: 'text/css',
      href: source
    });

    var load = properties.onload || properties.onLoad,
      doc = properties.document || document;

    delete properties.onload;
    delete properties.onLoad;
    delete properties.document;

    if (load) link.addEvent('load', load);
    return link.set(properties).inject(doc.head);
  },

  image: function(source, properties){
    if (!properties) properties = {};

    var image = new Image(),
      element = document.id(image) || new Element('img');

    ['load', 'abort', 'error'].each(function(name){
      var type = 'on' + name,
        cap = 'on' + name.capitalize(),
        event = properties[type] || properties[cap] || function(){};

      delete properties[cap];
      delete properties[type];

      image[type] = function(){
        if (!image) return;
        if (!element.parentNode){
          element.width = image.width;
          element.height = image.height;
        }
        image = image.onload = image.onabort = image.onerror = null;
        event.delay(1, element, element);
        element.fireEvent(name, element, 1);
      };
    });

    image.src = element.src = source;
    if (image && image.complete) image.onload.delay(1);
    return element.set(properties);
  },

  images: function(sources, options){
    sources = Array.from(sources);

    var fn = function(){},
      counter = 0;

    options = Object.merge({
      onComplete: fn,
      onProgress: fn,
      onError: fn,
      properties: {}
    }, options);

    return new Elements(sources.map(function(source, index){
      return Asset.image(source, Object.append(options.properties, {
        onload: function(){
          counter++;
          options.onProgress.call(this, counter, index, source);
          if (counter == sources.length) options.onComplete();
        },
        onerror: function(){
          counter++;
          options.onError.call(this, counter, index, source);
          if (counter == sources.length) options.onComplete();
        }
      }));
    }));
  }

};

